define(['cm', 'jquery'], function () {
    $('footer').load('../html/footer.html')
    class Share {
        constructor() {
            this.obj1 = {
                url: '../data/good.json'
            }
            this.list = []
            this.box2 = document.querySelector('.bigbox .box2')
            this.box1 = document.querySelector('.box1')
            this.box = document.querySelector('.box')
            this.tiger = document.querySelector('.tiger img')
            this.input = document.querySelector('.bigbox input')
            this.button = document.querySelector('.bigbox button')
            this.dataList = []
            this.money = document.querySelector('#sort .moeny')
            this.cart = document.querySelector('#sort .cart')
            this.number = document.querySelector('#sort .number')
            console.log(this.cart);
            this.tigers = ['../imgaes/shop/老虎.png', '../imgaes/shop/老虎2.png', '../imgaes/shop/老虎3.png']
            this.get()
            this.next()
            this.jude()
            // this.getIn()
            this.jump()
            this.search()
            // this.sort()
        }
        get() {
            if (getCookie('用户')) {
                $('.login').html('<a href="../html/getPrice.html"> <img src="../imgaes/index/购物车.png" alt=""></a>')
            }
            pAjaxGet(this.obj1).then(data => {
                var res = JSON.parse(data)
                // console.log(res) 
                this.list = res.slice(0, 54)
                Pagination({
                    ele: '#ele',
                    totalCount: this.list.length,
                    totalPage: 10,
                    currentPage: 1,
                    needTotalCount: true,

                    callback: (page) => {
                        console.log('当前是第' + page + '页')
                        // var str = `<div class="tiger">
                        //     < img src = "../../imgaes/shop/老虎.png" alt = "" >
                        //     // </div>`

                        this.tiger.src = this.tigers[page - 1]
                        var str = '<ul class="paging_list">'
                        for (var i = (page - 1) * 16; i < page * 16; i++) {
                            str += `<li>
                                  <p ><img src=${this.list[i].img_small_logo} data-id=${this.list[i].goods_id} class='picture'></p>
                                  <p class='content'>${this.list[i].title}</p>
                                  <p class='content'>销量:${this.list[i].goods_number}</p>
                                  <p class='miaosha'>秒杀</p>
                                  <p class='price'>价格:￥${this.list[i].current_price}</p>
                                  <p class='shopping'><span>￥${this.list[i].price}</span> <button data-id=${this.list[i].goods_id}>加入购物车</button></p>
                                  
                                   </li>`
                        }

                        str += '</ul>'
                        this.box.innerHTML = str
                        var ul = document.querySelector('.box ul')
                        this.getIn(ul)
                        this.sort()
                    }
                });
            })
        }
        getIn(ul) {
            var user = getCookie('用户')
            var users = JSON.parse(localStorage.getItem('curt'))||[]
            var jugement = users.findIndex((item) => {
                return item.userName == user
            })
            console.log(jugement);
            if (user) {
                console.log(jugement);
                // console.log(this.list)
                ul.addEventListener('click', (e) => {
                    var e = e || window.event
                    var target = e.target || e.srcElement
                    if (target.nodeName == 'BUTTON') {
                        let id = target.dataset.id
                        console.log(this.list[3].cart_number)
                        var clickData = this.list.find((item) => {
                            return item.goods_id == id
                        })
                        // console.log(clickData)
                        var Data = JSON.parse(localStorage.getItem('shop'+jugement)) || []
                        if (Data.length > 0) {
                            var jude = Data.some(item => {
                                return item.goods_id == id
                            })
                            if (jude) {
                                var i = Data.findIndex(item => {
                                    return item.goods_id == id
                                })
                                Data[i].cart_number++
                            } else {
                                clickData.cart_number = 1
                                Data.push(clickData)
                            }
                        } else {
                            clickData.cart_number = 1
                            Data.push(clickData)
                        }
                        localStorage.setItem('shop'+jugement, JSON.stringify(Data))
                        this.jump()
                    }
                    if (target.className == 'picture') {
                        let id = target.dataset.id
                        var detail = this.list.find((item) => {
                            return item.goods_id == id
                        })
                        console.log(detail);
                        localStorage.setItem('detail', JSON.stringify(detail))
                        self.location = 'details.html'
                    }

                })
            }
            else {
                alert('请您先登录噢')
                window.location.href = '../html/register.html'
            }
        }
        jump() {
            var user = getCookie('用户')
            var users = JSON.parse(localStorage.getItem('curt'))||[]
            var jugement = users.findIndex((item) => {
                return item.userName == user
            })
            let everyList = JSON.parse(localStorage.getItem('shop'+jugement)) || []
            // console.log(everyList)
            //  展示购物的数量
            let count = 0
            everyList.forEach((item) => {
                count += item.cart_number
            })
            console.log(count)
            this.box1.innerHTML = `商品共${count}件`
        }
        next() {
            this.box1.addEventListener('click', () => {
                self.location = 'getPrice.html'
                // console.log(111)
            })
        }
        search() {
            let fun = () => {
                this.box.value = null
                var str = '<ul class="paging_list">'
                this.dataList = this.list.filter(itme => {
                    return itme.title.includes(this.input.value)
                })
                // console.log(this.dataList);
                for (var i = 0; i < this.dataList.length; i++) {
                    str += `<li>
                          <p ><img src=${this.dataList[i].img_small_logo} data-id=${this.dataList[i].goods_id} class='picture'></p>
                          <p class='content'>${this.dataList[i].title}</p>
                          <p class='content'>销量:${this.list[i].goods_number}</p>
                          <p class='miaosha'>秒杀</p>
                          <p class='price'>价格:￥${this.dataList[i].current_price}</p>
                          <p class='shopping'><span>￥${this.dataList[i].price}</span> <button data-id=${this.list[i].goods_id}>加入购物车</button></p>
                          
                           </li>`
                }
                str += '</ul>'
                this.box.innerHTML = str
            }
            this.button.addEventListener('click', () => {
                fun()
                var ul = document.querySelector('.box ul')
                this.getIn(ul)
            })


        }
        jude() {
            console.log($('.login'))
            if (getCookie('用户')) {
                let cookie = getCookie('用户')
                $('.login').html(`<a href="../html/member.html">${cookie}<img src="../imgaes/index/钻石.png" alt=""></a>`)
            }
        }
        sort() {
            console.log(this.list)
            // 价格排序
            this.money.addEventListener('click', () => {

                // this.list.forEach((item, index) => {
                for (var j = 0; j < this.list.length - 1; j++) {
                    console.log(j)
                    for (var i = 0; i < this.list.length - j - 1; i++) {
                        console.log(typeof (Number((this.list[i].current_price))));
                        if (Number(this.list[i].current_price) > Number(this.list[i + 1].current_price)) {
                            // console.log(this.list[i].current_price);
                            var third = this.list[i]
                            this.list[i] = this.list[i + 1]
                            this.list[i + 1] = third
                        }
                    }
                    // list[j] = this.list[this.list.length - j - 1]
                }
                console.log(this.list);
                var str = '<ul class="paging_list">'
                for (var i = 0; i < this.list.length; i++) {
                    str += `<li>
                          <p ><img src=${this.list[i].img_small_logo} data-id=${this.list[i].goods_id} class='picture'></p>
                          <p class='content'>${this.list[i].title}</p>
                          <p class='content'>销量:${this.list[i].goods_number}</p>
                          <p class='miaosha'>秒杀</p>
                          <p class='price'>价格:￥${this.list[i].current_price}</p>
                          <p class='shopping'><span>￥${this.list[i].price}</span> <button data-id=${this.list[i].goods_id}>加入购物车</button></p>
                          
                           </li>`
                }
                str += '</ul>'
                this.box.innerHTML = str
                var ul = document.querySelector('.box ul')
                this.getIn(ul)
            })
            // 销量排序
            this.cart.addEventListener('click', () => {
                console.log(this.list[0].cart_number);
                for (var j = 0; j < this.list.length - 1; j++) {

                    for (var i = 0; i < this.list.length - j - 1; i++) {
                        // console.log(typeof (Number((this.list[i].current_price))));
                        if (Number(this.list[i].goods_number) > Number(this.list[i + 1].goods_number)) {
                            // console.log(this.list[i].current_price);
                            var third = this.list[i]
                            this.list[i] = this.list[i + 1]
                            this.list[i + 1] = third
                        }
                    }
                    // list[j] = this.list[this.list.length - j - 1]
                }
                console.log(this.list);
                var str = '<ul class="paging_list">'
                for (var i = 0; i < this.list.length; i++) {
                    str += `<li>
                              <p ><img src=${this.list[i].img_small_logo} data-id=${this.list[i].goods_id} class='picture'></p>
                              <p class='content'>${this.list[i].title}</p>
                              <p class='content'>销量:${this.list[i].goods_number}</p>
                              <p class='miaosha'>秒杀</p>
                              <p class='price'>价格:￥${this.list[i].current_price}</p>
                              <p class='shopping'><span>￥${this.list[i].price}</span> <button data-id=${this.list[i].goods_id}>加入购物车</button></p>
                              
                               </li>`
                }
                str += '</ul>'
                this.box.innerHTML = str
                var ul = document.querySelector('.box ul')
                this.getIn(ul)
            })
            // 默认排序
            this.number.addEventListener('click', () => {
                this.get()
            })
        }

    }
    new Share()


    // 分页器
    function Pagination(data) {
        var isInit = true
        var ele = data.ele
        $(ele + '>div').html('')
        var currentPage = data.currentPage ? data.currentPage : 1
        var callback = data.callback ? data.callback : function (pageNum) {
            console.log('当前是第' + pageNum + '页')
        }
        // 跳转指定页函数
        function inputPage() {
            if (!isInit) {
                if (currentPage == $(ele + ' input').val()) return $(ele + ' input').val('')
                currentPage = $(ele + ' input').val()
            }
            if (($(ele + ' input').val() > 9 && data.totalPage > 9) || (isInit && currentPage > 10)) {
                if (currentPage % 10 < 5) {
                    var a = (currentPage - currentPage % 10) / 10
                    var b = a - 1
                    var c = a + b
                    $(ele + ' ul').animate({ left: -160 * c })
                    if (currentPage - (Math.floor(data.totalPage / 10) * 10) >= 0 && currentPage - (Math.floor(data.totalPage / 10) * 10) <= 5) {
                        if (data.totalPage % 10 >= 5 && data.totalPage - currentPage <= data.totalPage - Math.floor(data.totalPage / 10) * 10 - 5) {
                            $(ele + ' .list_box').animate({ width: (data.totalPage - Math.floor(data.totalPage / 10) * 10 + 5) * 32 })
                        } else if (data.totalPage % 10 < 5 && data.totalPage - currentPage <= data.totalPage - Math.floor(data.totalPage / 10) * 10) {
                            $(ele + ' .list_box').animate({ width: (data.totalPage - Math.floor(data.totalPage / 10) * 10 + 5) * 32 })
                        } else {
                            $(ele + ' .list_box').width(320)
                        }
                    } else {
                        $(ele + ' .list_box').animate({ width: 320 })
                    }
                } else {
                    $(ele + ' ul').animate({ left: -160 * ((currentPage - currentPage % 10) / 5) })
                    if (currentPage - (Math.floor(data.totalPage / 10) * 10 + 5) >= 0 && currentPage - (Math.floor(data.totalPage / 10) * 10 + 5) <= 5) {
                        $(ele + ' .list_box').animate({ width: (data.totalPage - Math.floor(data.totalPage / 10) * 10) * 32 })
                    } else if (currentPage - (Math.floor(data.totalPage / 10) * 10) >= 0 && currentPage - (Math.floor(data.totalPage / 10) * 10) <= 5) {
                        $(ele + ' .list_box').animate({ width: (data.totalPage - Math.floor(data.totalPage / 10) * 10) * 32 })
                    } else {
                        $(ele + ' .list_box').animate({ width: 320 })
                    }
                }

            } else {
                $(ele + ' ul').animate({ left: 0 })
                $(ele + ' .list_box').animate({ width: 320 })
            }
            $(ele + ' input').val('')
            $(ele + ' ul li').eq(currentPage - 1).addClass('Pagination_active').siblings().removeClass('Pagination_active')
            callback(currentPage)
        }
        if (data.needTotalCount) {
            $(ele + ' .Pagination').append('<span class="total_num">共<i></i>条</span>')
            $(ele + ' .Pagination .total_num i').text(data.totalCount)
        }
        var PaginationLiList = ''
        if (data.totalCount != 0) {
            $(ele + ' .Pagination').append('<span class="first_page">首页</span>' +
                '<span class="prepage">上一页</span>' +
                '<div class="list_box clearfix"><ul></ul></div>' +
                '<span class="nextpage">下一页</span>' +
                '<span class="last_page">尾页</span>' +
                '<span class="total_page">共' + data.totalPage + '页</span>' +
                '<input type="text">' +
                '<span class="redir">跳转</span>')
            if (data.totalPage < 10) {
                $(ele + ' .Pagination .list_box').width(data.totalPage * 32)
            }
            inputPage()
            isInit = false
            for (var i = 0; i < data.totalPage; i++) {
                var j = i + 1
                if (i == currentPage - 1) {
                    var li = '<li class="Pagination_active" data-page="' + j + '">' + j + '</li>'
                } else {
                    var li = '<li data-page="' + j + '">' + j + '</li>'
                }
                PaginationLiList += li
            }
            $(ele + ' .Pagination ul').html('')
            $(ele + ' .Pagination ul').html(PaginationLiList)
            $(ele + ' .Pagination ul').width(data.totalPage * 32)
            // 页码点击跳到指定页数
            $(ele + ' .Pagination ul').on('click', 'li', function () {
                currentPage = $(this).data("page")
                if (!$(this).hasClass('Pagination_active')) {
                    $(this).addClass('Pagination_active').siblings().removeClass('Pagination_active')
                    if (((currentPage / 10) % 1 == 0 || ((currentPage / 5) % 1 == 0 && currentPage > 5)) && currentPage != data.totalPage) {
                        if (currentPage % 2 == 0) {
                            var a = currentPage / 10
                            var b = a - 1
                            var c = a + b
                            $(ele + ' .Pagination ul').animate({ left: -160 * c })
                        } else {
                            $(ele + ' .Pagination ul').animate({ left: -160 * ((currentPage / 5) - 1) })
                        }
                        if (data.totalPage - currentPage < 5) {
                            $(ele + ' .Pagination .list_box').animate({ width: (data.totalPage - currentPage + 5) * 32 })
                        }
                    }
                    if (((currentPage - 1) / 5) % 1 == 0 && currentPage > 5 && currentPage != data.totalPage) {
                        $(ele + ' .Pagination ul').animate({ left: -160 * (((currentPage - 1) / 5) - 1) })
                        if (data.totalPage - currentPage >= 5) {
                            $(ele + ' .Pagination .list_box').animate({ width: 320 })
                        }
                    }
                    callback(currentPage)
                }
            })
            // 跳到第一页
            $(ele + ' .Pagination .first_page').on('click', function () {
                if (currentPage != 1) {
                    $(ele + ' .Pagination ul li').eq(0).addClass('Pagination_active').siblings().removeClass('Pagination_active')
                    $(ele + ' .Pagination ul').animate({ left: 0 })
                    if (data.totalPage > 9) {
                        $(ele + ' .Pagination .list_box').animate({ width: 320 })
                    } else {
                        $(ele + ' .Pagination .list_box').animate({ width: data.totalPage * 32 })
                    }
                    currentPage = 1
                    callback(currentPage)
                }
            })
            // 跳到最后一页
            $(ele + ' .Pagination .last_page').on('click', function () {
                if (currentPage != data.totalPage) {
                    currentPage = data.totalPage
                    $(ele + ' .Pagination ul li').eq(data.totalPage - 1).addClass('Pagination_active').siblings().removeClass('Pagination_active')
                    if (data.totalPage > 10) {
                        if (data.totalPage % 10 <= 5) {
                            var a = (data.totalPage - data.totalPage % 10) / 10
                            var b = a - 1
                            var c = a + b
                            $(ele + ' .Pagination ul').animate({ left: -160 * c })
                            $(ele + ' .Pagination .list_box').stop().animate({ width: (data.totalPage - Math.floor(data.totalPage / 10) * 10 + 5) * 32 })
                        } else {
                            $(ele + ' .Pagination ul').animate({ left: -160 * ((data.totalPage - data.totalPage % 10) / 5) })
                            $(ele + ' .Pagination .list_box').stop().animate({ width: (data.totalPage - Math.floor(data.totalPage / 10) * 10) * 32 })
                        }
                    }
                    callback(currentPage)
                }
            })
            // 上一页
            $(ele + ' .Pagination .prepage').on('click', function () {
                if (currentPage > 1) {
                    currentPage--
                    if (((currentPage - 1) / 5) % 1 == 0 && currentPage > 5) {
                        $(ele + ' .Pagination ul').animate({ left: -160 * (((currentPage - 1) / 5) - 1) })
                        if (data.totalPage - currentPage >= 4) {
                            $(ele + ' .Pagination .list_box').animate({ width: 320 })
                        }
                    }
                    $(ele + ' .Pagination ul li').eq(currentPage - 1).addClass('Pagination_active').siblings().removeClass('Pagination_active')
                    callback(currentPage)
                }
            })
            // 下一页
            $(ele + ' .Pagination .nextpage').on('click', function () {
                if (currentPage < data.totalPage) {
                    if (currentPage > 9) {
                        if (((currentPage / 10) % 1 == 0 || ((currentPage / 5) % 1 == 0 && currentPage > 5)) && currentPage != data.totalPage) {
                            if (currentPage % 2 == 0) {
                                var a = currentPage / 10
                                var b = a - 1
                                var c = a + b
                                $(ele + ' .Pagination ul').animate({ left: -160 * c })
                            } else {
                                $(ele + ' .Pagination ul').animate({ left: -160 * ((currentPage / 5) - 1) })
                            }
                            if (data.totalPage - currentPage < 5) {
                                $(ele + ' .Pagination .list_box').animate({ width: (data.totalPage - currentPage + 5) * 32 })
                            }
                        }
                    }
                    currentPage++

                    $(ele + ' .Pagination ul li').eq(currentPage - 1).addClass('Pagination_active').siblings().removeClass('Pagination_active')
                    callback(currentPage)
                }
            })
            // 输入跳转指定页
            $(ele + ' .Pagination input').on('keydown', function (e) {
                if (e.keyCode == 13 && $(this).val() <= data.totalPage && $(this).val() != 0) {
                    inputPage()
                }
            })
            // 转跳按钮
            $(ele + ' .Pagination .redir').on('click', function () {
                if ($(ele + ' .Pagination input').val() <= data.totalPage && $(ele + ' input').val() != 0) {
                    inputPage()
                }
            })
        }
    }
})
var box = document.querySelector('.bigbox .box2')
var input = document.querySelector('.bigbox input')
var button = document.querySelector('.bigbox button')
function create() {
    let script = document.createElement('script')
    script.setAttribute('src', 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?cb=fu&wd=' + input.value)
    return script
}
input.addEventListener('input', () => {
    var c = create()
    document.body.appendChild(c)
})

function fu(data) {
    var a = data.s
    data.q = input.value
    var str = '<ul>'
    for (var i = 0; i < a.length; i++) {
        str += `<li>${a[i]}</li>`
    }
    str += '</ul>'
    box.innerHTML = str
    var li = document.querySelectorAll('.bigbox .box2 li')
    // console.log(li)
    li.forEach((item, index) => {
        item.addEventListener('mouseover', () => {
            item.style.backgroundColor = 'skyblue'
        })
        item.addEventListener('mouseout', () => {
            item.style.backgroundColor = '#f4f4f4'
        })
        item.addEventListener('click', () => {
            console.log(item.innerHTML);
            input.value = item.innerHTML
            for (var i = 0; i < li.length; i++) {
                li[i].style.display = 'none'
            }
        })
    })
    // console.log(li[0])
}